﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Reflection;
using System.Collections.Specialized;

/// <summary>
///AjaxPage 的摘要说明
/// </summary>
public class AjaxPage<T> : System.Web.UI.Page
{
    private static readonly string AjaxMethodCacheName = "AjaxMethods";
    private string pageName = typeof(T).Name;
    private Type pageType = typeof(T);

    protected override void OnPreInit(EventArgs e)
    {
        if (Cache[AjaxMethodCacheName] == null)
        {
            AjaxMethods = AjaxHelper.GetAjaxMethods<T>();
            Cache[AjaxMethodCacheName] = AjaxMethods;
        }
        else
        {
            AjaxMethods = Cache[AjaxMethodCacheName] as Dictionary<string, string>;
        }
        base.OnPreInit(e);
    }

    protected override void OnLoad(EventArgs e)
    {
        ExecuteAction();
        base.OnLoad(e);
    }

    /// <summary>
    /// Page对象中标记为Ajax的方法集合
    /// </summary>
    public Dictionary<string, string> AjaxMethods
    {
        get;
        set;
    }

    /// <summary>
    /// Action名称或者别名
    /// </summary>
    public string CurrentActionName
    {
        get
        {
            if (!string.IsNullOrEmpty(HttpContext.Current.Request["action"]))
            {
                return HttpContext.Current.Request["action"];
            }
            throw new Exception("调用时必须指定Action参数..");
        }
    }

    /// <summary>
    /// 传递的参数集合
    /// </summary>
    public object[] CurrentParams
    {
        //一般的调用URL格式为 http://xxxxx/yyy.aspx?action=method&id=a001 ...
        get
        {
            NameValueCollection NVs = HttpContext.Current.Request.QueryString;
            object[] objs = new object[NVs.Count - 1]; //这里将action参数排除在外
            for (int i = 1; i < NVs.Count; i++)
            {
                objs[i - 1] = NVs[i];
            }
            return objs;
        }
    }

    /// <summary>
    /// 最终要执行的Page的方法的真实名称
    /// </summary>
    public string CurrentAjaxMethodName
    {
        get;
        set;
    }


    public void ExecuteAction()
    {
        if (!AjaxMethods.ContainsKey(CurrentActionName) && !AjaxMethods.ContainsValue(CurrentActionName))
        {
            throw new Exception("调用的方法不存在或者未被公开为Ajax方法..");
        }

        //没有为Ajax方法指定别名
        if (AjaxMethods.ContainsKey(CurrentActionName) && AjaxMethods[CurrentActionName] == CurrentActionName)
        {
            CurrentAjaxMethodName = CurrentActionName;
        }
        else
        {
            //为Ajax方法指定了别名
            CurrentAjaxMethodName = AjaxMethods.First<KeyValuePair<string, string>>(x => x.Value == CurrentActionName).Key;
        }

        MethodInfo method = pageType.GetMethod(CurrentAjaxMethodName);
        if (method != null)
        {
            object PageObj = GetPageInstance();
            method.Invoke(PageObj, CurrentParams);
        }
    }

    /// <summary>
    /// 获取Page实例
    /// </summary>
    /// <returns></returns>
    public object GetPageInstance()
    {
        if (Cache[pageName] == null)
        {
            object PageObj = Activator.CreateInstance(pageType);
            return Cache[pageName] = PageObj;
        }
        else
        {
            return Cache[pageName] as object;
        }
    }
}